<script>
import { addArticleComment } from '@/api/homePage.js';
export default {
  name: "comments",
  data() {
    return {
      commentContent: '',
      articleId: ''
    }
  },
  onLoad(options) {
    this.articleId = options.id;
  },
  methods: {
    inputTextarea() {
      if (this.commentContent.length > 200) {
        uni.showToast({
          title: '最多200字',
          icon: 'none'
        });
        this.commentContent = this.commentContent.slice(0, 200);
      }
    },
    async submitComment() {
      if (!this.commentContent.trim()) {
        uni.showToast({
          title: '请输入评论内容',
          icon: 'none'
        });
        return;
      }
      
      uni.showLoading({ title: '提交中', mask: true });
      
      try {
        // 提交评论
        await addArticleComment({
          ArticleId: this.articleId,
          CommentText: this.commentContent,
          CommentImages: '' // 空字符串表示无图片
        });
        
        uni.hideLoading();
        uni.showToast({ title: '评论成功，等待审核', icon: 'success' });
        uni.navigateBack();
      } catch (e) {
        console.error('评论提交失败:', e);
        uni.hideLoading();
        uni.showToast({ title: '评论失败', icon: 'none' });
      }
    }
  }
}
</script>

<template>
  <view class="comment-contain">
    <textarea
        v-model="commentContent"
        placeholder="写下你的评论..."
        maxlength="200"
        @input="inputTextarea"
        class="comment-textarea"
    />
    
    <view class="submit_btn" @click="submitComment">发布评论</view>
  </view>
</template>

<style scoped lang="scss">
.comment-contain {
  width: 100vw;
  min-height: 100vh;
  padding: 30rpx;
  background-color: #f8f8f8;
  .comment-textarea {
    width: 100%;
    min-height: 180rpx;
    border-radius: 10rpx;
    border: 1rpx solid #eee;
    padding: 20rpx;
    font-size: 30rpx;
    margin-bottom: 20rpx;
    background: #fff;
  }
  
  .submit_btn {
    margin-top: 60rpx;
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    font-size: large;
    font-weight: bold;
    border-radius: 40rpx;
    color: #fff;
    text-align: center;
    background: linear-gradient(to left, #3a7fe6, #5895e9);
  }
}
</style>